﻿Imports DataSet_apaTableAdapters
Imports System.Data
Imports System.Data.SqlClient
Imports DataSet_apa
Imports System.Text

Partial Class Reportes_Filtros
    Inherits System.Web.UI.Page
    Dim RegLista As Integer
    Dim RLid As Integer
    Dim nombreLista As String
    Dim sSQL, campos As String
    Dim CampoBusqueda As String
    Dim NombreReporte As String
    Dim CampoRegNum As String = ""
    Dim sdsReporteCampos As New SqlDataSource

    Dim Ids As Integer

    Dim idFiltro As Integer
    Dim idFiltroG As Integer

    Dim filtros As New filtroDataTable
    Dim filtroAdapter As New filtroTableAdapter

    Dim filtrosDetalle As New filtroDetalleDataTable
    Dim filtrosDetalleAdapter As New filtroDetalleTableAdapter
    Protected Sub Reportes_Filtros_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        llenaGrid()

    End Sub
    Sub llenaGrid()
        Dim filtros As New filtroDataTable
        filtros = filtroAdapter.GetFiltro
        GridVFiltro.DataSource = filtros
        GridVFiltro.DataBind()
    End Sub
    Protected Sub GridVFiltro_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridVFiltro.RowDeleting
        idFiltro = GridVFiltro.DataKeys(e.RowIndex).Item("IdFiltro")
        sSQL = "DELETE FROM filtroDetalle where IdFiltro =" & idFiltro
        Try
            sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsReporteCampos.DeleteCommand = sSQL
            sdsReporteCampos.Delete()

        Catch ex As Exception
        End Try
        sSQL = "DELETE FROM Filtro where IdFiltro =" & idFiltro
        Try
            sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsReporteCampos.DeleteCommand = sSQL
            sdsReporteCampos.Delete()
        Catch ex As Exception
        End Try
        llenaGrid()
        TBDescripccionNuevo.Text = ""
        TBDescripccionEdita.Text = ""
        TBDescripccionNuevo.Enabled = False
        TBDescripccionEdita.Enabled = False
    End Sub

    Protected Sub GridVFiltro_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridVFiltro.RowEditing
        Dim CampoNombre As String
        idFiltro = GridVFiltro.DataKeys(e.NewEditIndex).Item("IdFiltro")
        TBDescripccionEdita.Visible = True
        TBDescripccionEdita.Enabled = True

        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        Try
            Dim Consulta As String = "Select * from Filtro where idfiltro=" + idFiltro.ToString
            OrdenSQL = New SqlCommand(Consulta, ConexionBD)
            Dim Lector As SqlDataReader = Nothing
            ConexionBD.Open()
            Lector = OrdenSQL.ExecuteReader()

            If Lector.Read() Then
                CampoNombre = RTrim(Lector("NombreFiltro"))

            End If


        Catch ex As Exception

        End Try
        TBDescripccionEdita.Text = CampoNombre
        ConexionBD.Close()
        TBDescripccionNuevo.Visible = False
        Dim filtrosDetalle As New filtroDetalleDataTable
        filtrosDetalle = filtrosDetalleAdapter.GetFiltroDetalleById(idFiltro)
        GridVDetalle.DataSource = filtrosDetalle
        GridVDetalle.DataBind()
    End Sub
    Protected Sub GridVFiltro_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridVFiltro.SelectedIndexChanged
        Dim codigo As String = ""
        Dim sUrl As String = Request.Url.Authority()

        Dim campoBusqueda As String = GridVFiltro.SelectedRow.Cells(1).Text
        Try

            idFiltro = GridVFiltro.SelectedIndex
            idFiltro = GridVFiltro.DataKeys(idFiltro).Item("IdFiltro")

            Session("filtro") = idFiltro
        Catch ex As Exception
        End Try


        If Len(idFiltro) = 0 Then
            Label2.Text = "Falta seleccionar "
        Else
            Dim numReg As Integer
            Dim filtrosDetalle As New filtroDetalleDataTable
            filtrosDetalle = filtrosDetalleAdapter.GetFiltroDetalleById(idFiltro)
            GridVDetalle.DataSource = filtrosDetalle
            numReg = filtrosDetalle.Count
            If numReg = 0 Then
                BtnAplica.Enabled = False
            Else
                BtnAplica.Enabled = True
            End If
            BtnCambiarnombre.Enabled = True
            BtnEliminar.Enabled = True
            BtnEditar.Enabled = True
            GridVDetalle.DataBind()
            Label2.Text = "Filtro seleccionado "
            TBDescripccionNuevo.Visible = False
            TBDescripccionEdita.Visible = True
            TBDescripccionEdita.Enabled = False
            TBDescripccionEdita.Text = campoBusqueda

        End If
    End Sub
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEditar.Click
        Dim sinSeleccion As Boolean = True
        Dim ruta As String = Request.ApplicationPath
        Dim codigo As String = ""
        Dim sUrl As String = Request.Url.Authority()
        Try
            idFiltro = GridVFiltro.SelectedIndex
            idFiltro = GridVFiltro.DataKeys(idFiltro).Item("IdFiltro")
        Catch ex As Exception
            sinSeleccion = False
        End Try

        If sinSeleccion = False Then
            Label2.Text = "Falta seleccionar algún filtro"
        Else
            sUrl = "http://" + sUrl
            codigo += sUrl & "/Santafe/Reportes/EdicionFiltro.aspx?opt=1&modulo=10&filtro=" + idFiltro.ToString
            Response.Redirect(codigo)
        End If
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAplica.Click
        Dim sUrl As String = Request.Url.Authority()
        Dim codigo As String = ""
        Label2.Text = ""
        Try
            idFiltro = GridVFiltro.SelectedIndex
            idFiltro = GridVFiltro.DataKeys(idFiltro).Item("IdFiltro")
        Catch ex As Exception
            idFiltro = 0
        End Try
        If idFiltro = 0 Then
            Label2.Text = "Falta seleccionar algún filtro "
            BtnEditar.Enabled = False
            BtnEliminar.Enabled = False
        Else
            Session("filtro") = idFiltro
            BtnEliminar.Enabled = True
            BtnEditar.Enabled = True
            sUrl = "http://" + sUrl
            codigo += sUrl & "/Santafe/registro/buscar.aspx?modulo=1&opt=1"
            Response.Redirect(codigo)
        End If

    End Sub
    Protected Sub BtnNuevo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnNuevo.Click
        TBDescripccionEdita.Visible = False
        TBDescripccionNuevo.Visible = True
        TBDescripccionNuevo.Enabled = True
        TBDescripccionNuevo.Focus()
        TBDescripccionNuevo.Text = ""
        Label2.Text = ""
        RegLista = 0
        BtnNuevo.Enabled = False
    End Sub

    Protected Sub TBDescripccionEdita_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TBDescripccionEdita.TextChanged
        Busqueda()
        idFiltroG = RegLista
        NombreReporte = Trim(TBDescripccionEdita.Text)
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        If idFiltroG = 0 Then
            TBDescripccionEdita.BackColor = Drawing.Color.Azure
            sSQL = "INSERT INTO Filtro (nombreFiltro) VALUES  ('" + NombreReporte + "') ; SELECT IdFiltro = SCOPE_IDENTITY()"
            Try
                sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
                sdsReporteCampos.InsertCommand = sSQL
                sdsReporteCampos.Insert()
            Catch ex As Exception

            End Try
        Else
            sSQL = "UPDATE Filtro SET  nombreFiltro =" & "'" & NombreReporte & "'" & " ,usrFiltro ='19069534-d40b-49a9-b533-759d424554a9' where idFiltro =" & RegLista
            Try
                sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
                sdsReporteCampos.UpdateCommand = sSQL
                sdsReporteCampos.Update()
            Catch ex As Exception

            End Try

        End If
        llenaGrid()
        TBDescripccionEdita.Text = ""
        TBDescripccionEdita.Enabled = False
        BtnNuevo.Enabled = True
    End Sub
    Protected Sub BtnEliminar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnEliminar.Click
        Busqueda()
        sSQL = "DELETE FROM filtroDetalle where IdFiltro =" & RegLista
        Try
            sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsReporteCampos.DeleteCommand = sSQL
            sdsReporteCampos.Delete()

        Catch ex As Exception
        End Try
        sSQL = "DELETE FROM Filtro where IdFiltro =" & RegLista
        Try
            sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsReporteCampos.DeleteCommand = sSQL
            sdsReporteCampos.Delete()
        Catch ex As Exception
        End Try
        llenaGrid()
        TBDescripccionEdita.Text = ""
        TBDescripccionNuevo.Text = ""
        Label2.Text = ""
    End Sub
    Sub Busqueda()
        Dim CampoReg As Integer
        Dim sinSeleccion As Boolean = True
        If NombreReporte = "" Then
            Try
                nombreLista = GridVFiltro.SelectedRow.Cells(1).Text
            Catch ex As Exception
                nombreLista = TBDescripccionNuevo.Text
            End Try
            If Len(nombreLista) = 0 Then
                sinSeleccion = False
            Else
                sinSeleccion = True
            End If
        Else
            nombreLista = NombreReporte
            sinSeleccion = True
        End If

        If sinSeleccion = True Then
            Dim CampoNombre As String
            Dim ConexionBD As New SqlConnection()
            ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            Dim OrdenSQL As SqlCommand = Nothing
            Try
                Dim Consulta As String = "Select * from Filtro order by idfiltro "
                OrdenSQL = New SqlCommand(Consulta, ConexionBD)
                Dim Lector As SqlDataReader = Nothing
                ConexionBD.Open()
                Lector = OrdenSQL.ExecuteReader()
                Dim salida As Integer
                While salida = 0
                    If Lector.Read() Then
                        CampoReg = Lector("IdFiltro")
                        CampoNombre = RTrim(Lector("NombreFiltro"))
                        If CampoNombre = RTrim(nombreLista) Then
                            RegLista = CampoReg
                        End If
                    Else
                        salida = 1
                    End If
                End While
                CampoReg = CampoReg + 1
                CampoRegNum = CType(CampoReg, String)
            Catch ex As Exception

            End Try
        End If
    End Sub
    Protected Sub GridVFiltro_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridVFiltro.PageIndexChanging
        ' procesos del grid
        Dim newPageNumber1 As Integer = e.NewPageIndex
        Dim filtros As New filtroDataTable
        filtros = filtroAdapter.GetFiltro
        GridVFiltro.PageIndex = newPageNumber1
        GridVFiltro.DataSource = filtros
        GridVFiltro.DataBind()
    End Sub
    Protected Sub GridVDetalle_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridVDetalle.PageIndexChanging
        ' procesos del grid
        Dim newPageNumber2 As Integer = e.NewPageIndex

        idFiltro = GridVFiltro.SelectedIndex
        idFiltro = GridVFiltro.DataKeys(idFiltro).Item("IdFiltro")


        Dim filtrosDetalle As New filtroDetalleDataTable
        filtrosDetalle = filtrosDetalleAdapter.GetFiltroDetalleById(idFiltro)
        GridVDetalle.PageIndex = newPageNumber2

        GridVDetalle.DataSource = filtrosDetalle
        GridVDetalle.DataBind()
    End Sub
    Protected Sub TBDescripccionNuevo_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TBDescripccionNuevo.TextChanged
        Dim codigo As String = ""
        Dim sUrl As String = Request.Url.Authority()
        NombreReporte = Trim(TBDescripccionNuevo.Text)
        Dim ConexionBD As New SqlConnection()
        ConexionBD.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
        Dim OrdenSQL As SqlCommand = Nothing
        TBDescripccionEdita.BackColor = Drawing.Color.Azure
        sSQL = "INSERT INTO Filtro (nombreFiltro) VALUES  ('" + NombreReporte + "') ; SELECT IdFiltro = SCOPE_IDENTITY()"
        Try
            sdsReporteCampos.ConnectionString = ConfigurationManager.ConnectionStrings("GanaderiaConnectionString").ConnectionString
            sdsReporteCampos.InsertCommand = sSQL
            sdsReporteCampos.Insert()
        Catch ex As Exception

        End Try

        Busqueda()
        idFiltro = RegLista
        Session("filtro") = idFiltro
        sUrl = "http://" + sUrl
        codigo += sUrl & "/Santafe/Reportes/EdicionFiltro.aspx?opt=1&modulo=10&filtro=" + idFiltro.ToString
        Response.Redirect(codigo)
    End Sub
    Protected Sub BtnCambiarnombre_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCambiarnombre.Click
        Dim NomFiltro As String
        Dim sinSeleccion As Boolean = True
        TBDescripccionEdita.Enabled = True
        NomFiltro = TBDescripccionEdita.Text
        If NomFiltro = "" Then
            Label2.Text = "Falta seleccionar algún filtro"
            BtnNuevo.Enabled = True
        Else
            BtnNuevo.Enabled = False
        End If
        'Try
        '    'idFiltro = GridVFiltro.SelectedIndex
        '    'idFiltro = GridVFiltro.DataKeys(idFiltro).Item("IdFiltro")
        'Catch ex As Exception
        '    sinSeleccion = False
        'End Try
        'If sinSeleccion = False Then
        '    Label2.Text = "Falta seleccionar algún filtro"
        '    BtnNuevo.Enabled = True
        'Else
        '    BtnNuevo.Enabled = False
        'End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridVFiltro.HeaderStyle.Height = 30
        GridVDetalle.Columns(1).HeaderStyle.Width = 100
        GridVDetalle.Columns(1).ItemStyle.Width = 100
        GridVDetalle.Columns(2).HeaderStyle.Width = 100
        GridVDetalle.Columns(2).ItemStyle.Width = 100
        GridVDetalle.RowStyle.Height = 19
        GridVDetalle.HeaderStyle.Height = 35
        GridVDetalle.PagerStyle.Height = 25
    End Sub
End Class
